package cn.cs.yangtai.controller;

import cn.cs.yangtai.entity.Comment;
import cn.cs.yangtai.entity.Comminicate;
import cn.cs.yangtai.entity.vo.CommentVo;
import cn.cs.yangtai.entity.vo.CommodityVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.entity.vo.ResponsePojo;
import cn.cs.yangtai.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 评论表
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2024-12-17 13:08:44
 */
@RestController
@CrossOrigin
@Tag(name = "评论")
@RequestMapping("comment")
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Operation(summary = "评论分页")
    @GetMapping
    public ResponsePojo<PageResult<CommentVo>> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageResult<CommentVo> result=commentService.queryByPage(cuttentPage,pageSize);
        return ResponsePojo.success(result);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "根据评论id查询评论")
    @GetMapping("{id}")
    public ResponseEntity<CommentVo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.commentService.queryById(id));
    }

    /**
     * 根据话题查询评论数据
     * @return 对象列表
     */
    @Operation(summary = "查询单个话题的评论")
    @GetMapping("/comments")
    public List<CommentVo> comments(Integer blogId) {
        return commentService.queryAllByBlogId(blogId);
    }

    /**
     * 新增数据
     *
     * @param comment 实体
     * @return 新增结果
     */
    @Operation(summary = "添加评论")
    @PostMapping
    public ResponseEntity<?> add(Integer commentBlogId,Integer commentUserId,String comment) {
        Comment comments=new Comment();
        comments.setCommentBlogId(commentBlogId);
        comments.setCommentUserId(commentUserId);
        comments.setComment(comment);
        comments.setCommentDate(new Date());
        commentService.insert(comments);
        return ResponseEntity.ok().body("评论添加成功");
    }

    /**
     * 编辑数据
     *
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<?> edit(Integer commentId,Integer commentUserId,String comment) {
        Comment existingComment = commentService.queryBy(commentId);
        if (existingComment == null) {
            return ResponseEntity.ok().body("评论不存在");
        }
        if (!Objects.equals(commentUserId, existingComment.getCommentUserId())) {
            return ResponseEntity.ok().body("您无权修改此评论");
        }
        existingComment.setComment(comment);
        existingComment.setCommentDate(new Date()); // Assuming the date should be updated on edit
        CommentVo result = commentService.update(existingComment);
        if (result != null) {
            return ResponseEntity.ok().body("修改成功");
        } else {
            return ResponseEntity.ok().body("修改失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Operation(summary = "根据评论id删除评论")
    @DeleteMapping
    public ResponseEntity<?> deleteById(Integer id,Integer commentUserId) {
        Comment comment = commentService.queryBy(id);
        if (comment == null) {
            return ResponseEntity.ok().body("评论不存在");
        }
        if (!Objects.equals(commentUserId, comment.getCommentUserId())) {
            return ResponseEntity.ok().body("您无权删除此评论");
        }
        boolean result = commentService.deleteById(id);
        if (result) {
            return ResponseEntity.ok().body("删除成功");
        } else {
            return ResponseEntity.ok().body("删除失败");
        }
    }

}

